package com.ksyun.ks3.demo;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.ksyun.ks3.dto.CannedAccessControlList;
import com.ksyun.ks3.dto.InitiateMultipartUploadResult;
import com.ksyun.ks3.dto.ListPartsResult;
import com.ksyun.ks3.dto.ObjectMetadata;
import com.ksyun.ks3.dto.PartETag;
import com.ksyun.ks3.service.request.CompleteMultipartUploadRequest;
import com.ksyun.ks3.service.request.InitiateMultipartUploadRequest;
import com.ksyun.ks3.service.request.ListPartsRequest;
import com.ksyun.ks3.service.request.UploadPartRequest;
import com.ksyun.ks3.utils.Timer;

/**
 * @author lijunwei[lijunwei@kingsoft.com]  
 * 
 * @date 2014年12月1日 下午4:02:58
 * 
 * @description 
 **/
public class MultipartUploadDemo extends AbstractDemo{
	public void multipartUploadWithFile(){
		//定义每次上传的块的大小为10M
		long part = 10 * 1024 * 1024;
		//将文件存放在test.bucket这个存储空间里
		String bucket = "test.bucket";
		//将要上传的文件的object key
		String key = "object.rar";
		//将要上传的文件路径
		String filename = "filePath";

		
		//***********************初始化分块上传*****************************************
		InitiateMultipartUploadRequest request1 = new InitiateMultipartUploadRequest(
				bucket, key);
		//设置为公开读
		request1.setCannedAcl(CannedAccessControlList.PublicRead);
		ObjectMetadata meta = new ObjectMetadata();
		//设置将要上传的object的用户元数据
		meta.setUserMeta("x-kss-meta-example", "example");
		//设置将要上传的object的元数据
		meta.setContentType("text/html");
		meta.setContentEncoding("gzip");
		meta.setCacheControl("no-cache");
		meta.setHttpExpiresDate(new Date());
		meta.setContentDisposition("attachment; filename=fname.ext");
		//设置元数据
		request1.setObjectMeta(meta);
		
		InitiateMultipartUploadResult result = client
				.initiateMultipartUpload(request1);
		//***********************执行分块上传*****************************************
		File file = new File(filename);
		//计算一共有多少块
		long n = file.length() / part;
		//依次进行上传
		for (int i = 0; i <= n; i++) {
			//初始化一个分块上传的请求
			UploadPartRequest request = new UploadPartRequest(
					result.getBucket(), result.getKey(), result.getUploadId(),
					i + 1, file, part, (long) i * part);
			//可以指定内容的MD5值，否则程序只会在客户端进行MD5校验。如果指定的话会在服务端进行MD5校验
			request.setContentMd5("52D04DC20036DBD8");
			client.uploadPart(request);
		}
		//***********************列出分块上传以上传的块*****************************************
		ListPartsRequest requestList = new ListPartsRequest(result.getBucket(),
				result.getKey(), result.getUploadId());
		ListPartsResult tags = client.listParts(requestList);
		//***********************完成分块上传，使服务端将块合并成一个文件*****************************************
		CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(
				tags);
		client.completeMultipartUpload(request);
	}
	public void multipartUploadWithInputStream(){
		//将文件存放在test.bucket这个存储空间里
		String bucket = "ksc-scm";
		//将要上传的文件的object key
		String key = "object.txt";

		
		//***********************初始化分块上传*****************************************
		InitiateMultipartUploadRequest request1 = new InitiateMultipartUploadRequest(
				bucket, key);
		//设置为公开读
		request1.setCannedAcl(CannedAccessControlList.PublicRead);
		ObjectMetadata meta = new ObjectMetadata();
		//设置将要上传的object的用户元数据
		meta.setUserMeta("x-kss-meta-example", "example");
		//设置将要上传的object的元数据
		meta.setContentType("text/html");
		meta.setContentEncoding("gzip");
		meta.setCacheControl("no-cache");
		meta.setHttpExpiresDate(new Date());
		meta.setContentDisposition("attachment; filename=fname.ext");
		//设置元数据
		request1.setObjectMeta(meta);
		
		InitiateMultipartUploadResult result = client
				.initiateMultipartUpload(request1);
		//***********************执行分块上传*****************************************
		//假设一共要上传500块
		long n = 500;
		//依次进行上传
		for (int i = 0; i < n; i++) {
			//生成一个大小为5M的输入流
			StringBuffer buffer = new StringBuffer();
			for(int j = 0;j < 1024*1024;j++){
				buffer.append("12345");
			}
			InputStream content = new ByteArrayInputStream(buffer.toString().getBytes());
			//初始化一个分块上传的请求
			UploadPartRequest request = new UploadPartRequest(
					result.getBucket(), result.getKey(), result.getUploadId(),
					i + 1,content,5*1024*1024);
			//可以指定内容的MD5值，否则程序只会在客户端进行MD5校验。如果指定的话会在服务端进行MD5校验
			//request.setContentMd5("52D04DC20036DBD8");
			client.uploadPart(request);
		}
		//***********************列出分块上传以上传的块*****************************************
		ListPartsRequest requestList = new ListPartsRequest(result.getBucket(),
				result.getKey(), result.getUploadId());
		ListPartsResult tags = client.listParts(requestList);
		//***********************完成分块上传，使服务端将块合并成一个文件*****************************************
		CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(
				tags);
		client.completeMultipartUpload(request);
	}
}
